package mr.fileToFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class S1MMEMain extends Configured implements Tool {
	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			return;
		}
		ToolRunner.run(new S1MMEMain(), args);
	}

	@Override
	public int run(String[] arg0) throws Exception {
		Configuration conf = new Configuration();
		long t = System.currentTimeMillis();
		int delay = Integer.valueOf(arg0[0]);
		final Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, -delay);
		final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd/HH");
		final String time = format.format(cal.getTime());
		conf.set("nowtime", time.replace("/", ""));
		final Job job = Job.getInstance(conf, "s1mmefilter");

		job.setJarByClass(S1MMEMain.class);
		job.setMapperClass(S1MMEMapper.class);
		job.setReducerClass(S1MMEReducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		job.setInputFormatClass(TextInputFormat.class);
		job.setOutputFormatClass(S1MMEOutputFormat.class);

		job.setNumReduceTasks(3);
		job.setMaxMapAttempts(3);
		job.setMaxReduceAttempts(3);

		try {
//			FileInputFormat.addInputPath(job, new Path("E:\\lte\\s1mme"));
//			FileOutputFormat.setOutputPath(job, new Path("E:\\lte\\f2\\7"));
			FileInputFormat.addInputPath(job, new Path("hdfs://bjdpi2cluster/kqdata/REALTIME/S1MME/" + time));
			FileOutputFormat.setOutputPath(job, new Path("hdfs://bjdpi2cluster/mrs1mme/" + time));
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		int exitFlag = job.waitForCompletion(true) ? 0 : 1;
		System.out.println((System.currentTimeMillis()) - t);
		System.exit(exitFlag);
		return 0;
	}
}